<?php

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$setting=$this->setting;
$tabs=array(
	array('op'=>'list','title'=>'员工部门管理'),
	array('op'=>'edit','title'=>'添加/编辑部门'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']);
    $orderby='id desc';
	$total='';
	$return['list']=$this->getall('bumen',$params,array(),'',$orderby);
	foreach($return['list'] as &$item){
		
	}
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：
		</br>使用场景：所有员工，都是后台指定开通的；
		','edit'=>false,'list'=>array(
			array('name'=>'title','title'=>'部门名称','type'=>'string'),
		),'action'=>array(
			array('op'=>'edit','title'=>'编辑','type'=>'href'),
		)),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=$this->get('bumen',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array(
			'title'=>'',
		);
	}
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'部门设置',
		'edits'=>array(
			array('title'=>'基本信息','list'=>array(
			array('name'=>'title','title'=>'部门名称','type'=>'string'),
		))),
	);
	$return['item']=$item;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
	);
	if(!$data['title']){
		exi('请填写部门名称','error');
	}
	$id=intval($postdata['id']);
	if($id){
		$this->update('bumen',$data,array('id'=>$id));
		exi('设置成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$_SESSION['onemerchid'];
		$this->insert('bumen',$data);
		exi('添加成功！','','list');
	}
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		$this->delete('bumen',array('id'=>$id));	
	}
	if(!$id){
		exi('默认部门不能删除！','error');
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->delete('bumen',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>